package com.psn.zjk.utils.nexus_uploader.bean;

import java.io.File;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "project")
@XmlAccessorType(XmlAccessType.FIELD)
public class Pom {

    @XmlElement(name = "parent")
    private Pom parent;

    @XmlElement(name = "groupId")
    private String groupId;

    @XmlElement(name = "artifactId")
    private String artifactId;

    @XmlElement(name = "version")
    private String version;

    @XmlElement(name = "packaging")
    private String packaging;

    private File file;

    private File pom;

    private File source;

    private File javadoc;

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public File getSource() {
        return source;
    }

    public void setSource(File source) {
        this.source = source;
    }

    public File getJavadoc() {
        return javadoc;
    }

    public void setJavadoc(File javadoc) {
        this.javadoc = javadoc;
    }

    public Pom getParent() {
        return parent;
    }

    public String getGroupId() {
        if (groupId == null && parent != null) {
            groupId = parent.getGroupId();
        }
        return groupId;
    }

    public String getArtifactId() {
        return artifactId;
    }

    public String getVersion() {
        if (version == null && parent != null) {
            version = parent.getVersion();
        }
        return version;
    }

    public boolean valid() {
        return getArtifactId() != null && getGroupId() != null && getVersion() != null;
    }

    @Override
    public String toString() {
        StringBuffer string = new StringBuffer().append(getGroupId()).append("-").append(getArtifactId()).append("-")
                .append(getVersion());
        return string.toString();
    }

    public String getFileName() {
        StringBuffer string = new StringBuffer().append(getArtifactId()).append("-").append(getVersion()).append(".")
                .append(getPackaging());
        return string.toString();
    }

    public File getPom() {
        return pom;
    }

    public void setPom(File pom) {
        this.pom = pom;
    }

    public String getPackaging() {
        if (packaging == null||"bundle".equals(packaging)) {
            packaging = "jar";
        }
        return packaging;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Pom) {
            Pom tmp = (Pom) obj;
            return tmp.toString().equals(this.toString());
        }
        return super.equals(obj);
    }
}
